﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Database;

namespace ConnectDatabase
{
    public class GeoCountryDB
    {
        DatabaseDataContext dbContext = new DatabaseDataContext();

        #region Các hàm cơ bản
        public List<tblGeoCountry> GetAll()
        {
            try
            {
                var list = from a in dbContext.tblGeoCountries where a.Visible select a;
                return list.ToList<tblGeoCountry>();
            }
            catch
            {
                return new List<tblGeoCountry>();
            }
        }

        public tblGeoCountry GetByKey(string code)
        {
            try
            {
                var obj = dbContext.tblGeoCountries.SingleOrDefault(a => a.Code == code && a.Visible);
                return obj;
            }
            catch
            {
                return null;
            }
        }

        public bool Insert(tblGeoCountry obj)
        {
            bool isOk = false;
            try
            {
                dbContext.tblGeoCountries.InsertOnSubmit(obj);
                dbContext.SubmitChanges();
                isOk = true;
            }
            catch
            {
                return false;
            }
            return isOk;
        }

        public bool Update(tblGeoCountry obj)
        {
            bool isOk = false;
            try
            {
                var exist = dbContext.tblGeoCountries.SingleOrDefault(a => a.Code == obj.Code);
                if (exist != null)
                {
                    dbContext.SubmitChanges();
                    isOk = true;
                }
            }
            catch
            {
                isOk = false;
            }
            return isOk;
        }

        public bool Delete(string code)
        {
            bool isOk = false;
            try
            {
                var exist = dbContext.tblGeoCountries.SingleOrDefault(a => a.Code == code);
                if (exist != null)
                {
                    exist.Visible = false;
                    dbContext.SubmitChanges();
                    isOk = true;
                }
            }
            catch
            {
                isOk = false;
            }
            return isOk;
        }
        #endregion

        public List<tblGeoCountry> GetByContinentCode(string continentCode)
        {
            try
            {
                if (string.IsNullOrEmpty(continentCode))
                {
                    var list = from a in dbContext.tblGeoCountries where a.Visible select a;
                    return list.ToList<tblGeoCountry>();    
                }
                else
                {
                    var list = from a in dbContext.tblGeoCountries where a.ContinentCode == continentCode && a.Visible select a;
                    return list.ToList<tblGeoCountry>();
                }
            }
            catch
            {
                return new List<tblGeoCountry>();
            }
        }
    }
}
